/*
J_scroll

v0.9

*/
define(function(require, exports, module) {
   var $ = require('$');
	require('./scroll.css');
	
	$.fn.J_scroll=function(){
		var $this=$(this),
			 defaults={
				num:5,
				auto:true,
				time:4000,
				speed:300,
				step:1,
				node:'li',
				fn:null
				},
			 dataOpt=$this.data('scroll')||{},
			 opts=$.extend({},defaults,dataOpt),
			 $i=0,
			 $list=$this.find(opts.node),
			 $n=$list.length,
			 $max=Math.floor($this.width()/$list.outerWidth(true)),
			 $shown=opts.num > $max ? $max :opts.num,
			 $margin_lr=Math.round(($this.width()-$list.outerWidth(true)*$shown)/$shown/2*10)/10,
			 $aWid=$list.outerWidth(true)+$margin_lr*2,
			 $T_pre,
			 $T_nex,
			 $timer,
			 ani=function($i_){$this.children('.gallery').stop().animate({marginLeft:-$aWid*$i_},opts.speed)};
		$list.css('margin-left',$margin_lr).css('margin-right',$margin_lr);
		if($n<=$shown) return;
		if(!$this.parent().find('.s_prev').length) {$this.before($('<i class="s_prev" onselectstart="return false">&lt;</i>')).after('<i class="s_next" onselectstart="return false">&gt;</i>')};
		$T_pre=$this.parent().find('.s_prev');
		$T_nex=$this.parent().find('.s_next');
		if(opts.auto){
			$timer=setInterval(function(){$T_nex.click()}, opts.time);
			$this.hover(function(){clearInterval($timer)},function(){$timer=setInterval(function(){$T_nex.click()}, opts.time)})
		};
		$T_nex.click(function(event){
			event.preventDefault();
			$i=$n-$i-$shown>=opts.step ? $i+opts.step : ($n-$i-$shown===0 ? 0 : $n-$shown);
			ani($i);
			});
		$T_pre.click(function(event){
			event.preventDefault();
			$i=$i>=opts.step ? $i-opts.step : ($i===0 ? $n-$shown : 0);
			ani($i);
			});
		if(opts.fn){eval(opts.fn+"($this,$max,opts,$T_pre,$T_nex,ani)")}
	};
	

});